package activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.ci_edit.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import entity.UserConfig;
import okhttp3.Call;
import model.*;
import okhttp3.OkHttpClient;

public class LoginActivity extends AppCompatActivity {
    public static SharedPreferences.Editor editor;
    private EditText username, password;
    private Button loginBtn, registerBtn, forgetBtn, touristBtn;
    private SharedPreferences sharedPreferences;
    private CheckBox cbShow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        Intent intent = getIntent();
        //final boolean zhuxiao = intent.getBooleanExtra("zhuxiao", false);
        boolean zhuxiao = intent.getBooleanExtra("zhuxiao", false);
        if (zhuxiao) {
            editor.clear();
        }

        setContentView(R.layout.activity_login);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10000L, java.util.concurrent.TimeUnit.MILLISECONDS)
                .readTimeout(10000L, java.util.concurrent.TimeUnit.MILLISECONDS)
                .build();

        OkHttpUtils.initClient(okHttpClient);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        loginBtn = (Button) findViewById(R.id.loginBtn);
        registerBtn = (Button) findViewById(R.id.registerBtn);
        forgetBtn = (Button) findViewById(R.id.forgetBtn);
        touristBtn = (Button) findViewById(R.id.touristBtn);
        cbShow = (CheckBox) findViewById(R.id.cbShowPsw);

//        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
//        editor = sharedPreferences.edit();
        /*
         * 注册按钮监听
         */
        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
        /*
         * 忘记密码按钮监听
         */
        forgetBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.setClass(LoginActivity.this, ForgetPasswordActivity.class);
                startActivity(intent1);
            }
        });

        touristBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserConfig.setIslogin(false);
                UserConfig.setUsername("游客");
                Intent intent1 = new Intent();
                intent1.setClass(LoginActivity.this, SupperActivity.class);
                startActivity(intent1);
            }
        });

        /*
         * 显示密码按钮监听
         */
        cbShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    //如果选中，显示密码
                    password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    password.setTransformationMethod(PasswordTransformationMethod.getInstance());

                }

            }
        });
/*
 *登录按钮监听
 */
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String name = username.getText().toString();
                UserConfig.setUsername(name);
                final String pass = password.getText().toString();

                if (null == name || name.length() <= 0 || pass == null || pass.length() <= 0) {
                    username.requestFocus();
                    Toast.makeText(LoginActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                } else {

                    OkHttpUtils.post()
                            .url(Constants.SERVER_URL + "login")
                            .addParams("username", name)
                            .addParams("password", pass)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {

                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    int result = Integer.parseInt(response);
                                    if (result > 0) {
                                        editor.putBoolean("isLogin", true);
                                        editor.putString("username", name);
                                        editor.putInt("uid", result);
                                        //vip
                                        editor.putBoolean("isVip", false);
                                        editor.commit();

                                        UserConfig.setIslogin(true);
                                        UserConfig.setUsername(name);
                                        Intent intent = new Intent(LoginActivity.this, SupperActivity.class);
                                        startActivity(intent);
                                        finish();
                                    } else if (-1 == result) {
                                        Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                                    } else if (-2 == result) {
                                        Toast.makeText(LoginActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
                                    } else if (-3 == result) {
                                        Toast.makeText(LoginActivity.this, "验证失败，请稍后再试", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                }
            }
        });
    }

}
